<template>
  <a-modal
    :title="model && model.id > 0 ? '编辑员工' : '新增员工'"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="
      () => {
        $emit('ok')
      }
    "
    @cancel="
      () => {
        $emit('cancel')
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-form-item label="id" v-show="false">
          <a-input v-decorator="['id']" name="id" />
        </a-form-item>
        <a-form-item label="姓名">
          <a-input v-decorator="['userName', { rules: [{ required: true, message: '请输入姓名' }] }]" placeholder="请输入姓名" name="userName" />
        </a-form-item>
        <a-form-item label="电话">
          <a-input
            v-decorator="[
              'phone',
              {
                rules: [
                  { required: true, message: '请输入电话' },
                  { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' },
                ],
              },
            ]"
            placeholder="请输入电话"
            name="phone"
          />
        </a-form-item>
        <a-form-item label="身份证">
          <a-input
            v-decorator="[
              'idCard',
              {
                rules: [
                  { required: true, message: '请输入身份证' },
                  { pattern: /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/, message: '请输入正确的身份证', trigger: 'blur' },
                ],
              },
            ]"
            placeholder="请输入姓名"
            name="idCard"
          />
        </a-form-item>
        <a-form-item label="入职日期">
          <a-date-picker v-decorator="['inDate']" style="width: 100%" name="inDate" valueFormat="YYYY-MM-DD"/>
        </a-form-item>
        <a-form-item label="归属店">
          <a-select v-decorator="['storeId']" placeholder="请选择" name="storeId">
            <a-select-option :value="i.id" v-for="i in this.storeOption" :key="i.value">{{
              i.storeName
            }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="职务">
          <a-select v-decorator="['jobId']" placeholder="请选择" name="jobId">
            <a-select-option :value="i.id" v-for="i in this.option" :key="i.value">{{ i.jobName }}</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { getStoreListUsingPOST } from '@/api/api/mendianguanli.ts'
import { getEmJobsListUsingPOST } from '@/api/api/yuangongguanli.ts'

// 表单字段
const fields = ['userName', 'phone', 'empType', 'idCard', 'inDate', 'storeId', 'jobId', 'id']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    model: {
      type: Object,
      default: () => null,
    },
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 },
      },
    }
    return {
      storeOption: [],
      option: [],
      queryParam: {
        pageNo: 1,
        pageSize: 999,
        queryCondition: '',
        status: 1,
      },
      form: this.$form.createForm(this),
    }
  },
  created() {
    this.handleStore()
    this.handleOption()
    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  methods: {
    handleStore() {
      getStoreListUsingPOST(this.queryParam)
        .then((res) => {
          this.storeOption = res.data.list
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    handleOption() {
      getEmJobsListUsingPOST(this.queryParam)
        .then((res) => {
          this.option = res.data.list
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
  },
}
</script>
